{include file="common/header"}
<body class="pear-container">
<div class="layui-row layui-col-space10">
    <div class="layui-col-md12">
        <div class="layui-card">
            <div class="layui-card-body">
                <form class="layui-form top-search-from" action="">
                    <div class="layui-form-item layui-inline">
                        <label class="layui-form-label">考卷名称</label>
                        <div class="layui-input-block">
                            <input type="text" name="title" placeholder="请输入考卷名称" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item layui-inline">
                        <button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="query">
                            <i class="layui-icon layui-icon-search"></i>查询
                        </button>
                        <button type="reset" class="pear-btn pear-btn-md">
                            <i class="layui-icon layui-icon-refresh"></i>重置
                        </button>
                    </div>
                    <div class="toggle-btn">
                        <a class="layui-hide">展开<i class="layui-icon layui-icon-down"></i></a>
                        <a class="layui-hide">收起<i class="layui-icon layui-icon-up"></i></a>
                    </div>
                </form>
            </div>
        </div>
        <div class="layui-card">
            <div class="layui-card-body">
                <table id="examination-list-table" lay-filter="examination-list-table"></table>
            </div>
        </div>
    </div>
</div>

</body>
<script type="text/html" id="examination-list-toolbar">
    <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="add">
        <i class="layui-icon layui-icon-add-1"></i>
        新增
    </button>
</script>
<script type="text/html" id="examination-list-bar">
    <button class="pear-btn pear-btn-primary pear-btn-xs" lay-event="edit">
        <i class="layui-icon">编辑</i>
    </button>
    <button class="pear-btn pear-btn-xs" lay-event="more">
        <i class="layui-icon">更多</i>
    </button>
</script>
<script type="text/html" id="status">
    <input type="checkbox" name="status" value="{{d.id}}" lay-skin="switch" lay-text="启用|禁用"
           lay-filter="changeStatus"
           {{# if(d.status==1){ }} checked {{# } }}>
</script>
{include file="common/footer"}
<script>
    layui.use(['table', 'form', 'jquery', 'common', 'dropdown'], function () {
        let table = layui.table;
        let form = layui.form;
        let $ = layui.jquery;
        let common = layui.common;
        let dropdown = layui.dropdown;

        let MODULE_PATH = "{$Request.root}/Examination/";

        table.render({
            elem: '#examination-list-table',
            url: MODULE_PATH + 'index',
            cols: [
                [
                    {title: 'ID', field: 'id', align: 'center', width: 60},
                    {title: '考卷名称', field: 'title', align: 'center'},
                    {title: '考卷分类', field: 'category_title', align: 'center'},
                    {
                        title: '考卷图片', field: 'thumb', align: 'center', templet: function (d) {
                            if (d.thumb) return '<img class="photo" lay-event="photo" src=" ' + d.thumb + '">';
                            else return '';
                        }
                    },
                    {title: '考题数量', field: 'numbers', align: 'center'},
                    {title: '考卷总分', field: 'scores', align: 'center'},
                    {
                        title: '考卷难度', field: 'difficulty', align: 'center', templet: function (d) {
                            return d.difficulty + '星';
                        }
                    },
                    {title: '答题时间(m)', field: 'answer_time', align: 'center'},
                    {title: '创建时间', field: 'create_time', align: 'center'},
                    {title: '操作', toolbar: '#examination-list-bar', align: 'center', width: 135}
                ]
            ],
            cellMinWidth: 100,
            skin: 'grid',
            even: 'true',
            size: 'lg',
            page: true,
            toolbar: '#examination-list-toolbar',
            defaultToolbar: [{
                title: '刷新',
                layEvent: 'refresh',
                icon: 'layui-icon-refresh',
            }, 'filter', 'exports']
        });

        form.on('submit(query)', function (data) {
            table.reload('examination-list-table', {
                where: data.field,
                page: {curr: 1}
            })
            return false;
        });

        table.on('toolbar(examination-list-table)', function (obj) {
            if (obj.event === 'add') {
                window.edit(0);
            } else if (obj.event === 'refresh') {
                window.refresh();
            }
        });

        table.on('tool(examination-list-table)', function (obj) {
            if (obj.event === 'edit') {
                window.edit(obj.data['id']);
            } else if (obj.event === 'photo') {
                window.photo(obj.data['thumb']);
            } else if (obj.event === 'more') {
                dropdown.render({
                    elem: this, // 触发事件的 DOM 对象
                    show: true, // 外部事件触发即显示
                    data: [{
                        title: '考卷试题',
                        id: 'questions'
                    }, {
                        title: '删除考卷',
                        id: 'remove'
                    }],
                    click: function (menudata) {
                        if (menudata.id === 'questions') {
                            window.questions(obj.data['id']);
                        } else if (menudata.id === 'remove') {
                            window.remove(obj);
                        }
                    },
                    align: 'right', // 右对齐弹出
                    style: 'box-shadow: 1px 1px 10px rgb(0 0 0 / 12%);' // 设置额外样式
                })
            }
        });

        window.photo = function (url) {
            if (!url || url == "") {
                layer.msg("图片地址错误！");
                return;
            }
            var auto_img = {};
            var img = new Image();
            img.src = url;
            img.onload = function () {
                var max_height = $(window).height() - 100;
                var max_width = $(window).width();
                var rate1 = max_height / img.height;
                var rate2 = max_width / img.width;
                var rate3 = 1;
                var rate = Math.min(rate1, rate2, rate3);
                auto_img.height = img.height * rate;
                auto_img.width = img.width * rate;
                layer.open({
                    type: 1,
                    title: false,
                    area: ['auto'],
                    skin: 'layui-layer-nobg', //没有背景色
                    shadeClose: true,
                    content: "<img src='" + url + "' width='" + auto_img.width + "px' height='" + auto_img.height + "px'>"
                })
            }
        };

        window.edit = function (id) {
            layer.open({
                type: 2,
                maxmin: true,
                title: id === 0 ? '添加考卷' : '编辑考卷',
                shade: 0.1,
                area: common.screenSmall(),
                content: MODULE_PATH + 'edit/id/' + id
            });
        }

        window.questions = function (id) {
            layer.open({
                type: 2,
                maxmin: true,
                title: '编辑考题',
                shade: 0.1,
                area: common.screenLongMid(),
                content: MODULE_PATH + 'questions/pid/' + id,
                end: function () {
                    window.refresh();
                }
            });
        }

        window.remove = function (obj) {
            layer.confirm('确定要删除该数据?', {
                icon: 3,
                title: '提示'
            }, function (index) {
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url: MODULE_PATH + 'goRemove',
                    data: {id: obj.data['id']},
                    dataType: 'json',
                    type: 'POST',
                    success: function (res) {
                        layer.close(loading);
                        if (res.code == 200) {
                            layer.msg(res.msg, {
                                icon: 1,
                                time: 1000
                            }, function () {
                                obj.del();
                            });
                        } else {
                            layer.msg(res.msg, {
                                icon: 2,
                                time: 1000
                            });
                        }
                    }
                })
            });
        }

        window.refresh = function () {
            table.reload('examination-list-table');
        }
    })
</script>
</html>
